Is there a Max function in SQL Server that takes two values like Math.Max in .NET?
Is there a Max function in SQL Server that takes two values like Math.Max in .NET?
413
11-Jul-2023
Updated on 12-Jul-2023
Aryan Kumar
12-Jul-2023No, there is no built-in MAX function in SQL Server that takes two values. The MAX function only takes one parameter, and it returns the maximum value of all the values in a column.
However, there are a few ways to get the maximum of two values in SQL Server. One way is to use the following code:
SQL
This code will return the maximum value of the two values
value1andvalue2.Another way to get the maximum of two values in SQL Server is to use a CASE statement. The following code shows how to use a CASE statement to get the maximum of two values:
SQL
This code will return the maximum value of the two values
value1andvalue2. Ifvalue1is greater thanvalue2, then themavaluecolumn will contain the value ofvalue1. Otherwise, themavaluecolumn will contain the value ofvalue2.Finally, you can also use a user-defined function (UDF) to get the maximum of two values in SQL Server. The following code shows how to create a UDF to get the maximum of two values:
SQL
This code creates a UDF called
maof_twothat takes two integer parameters and returns the maximum value of the two parameters.